package _binary_search;

/**
 * 33. 搜索旋转排序数组
 */
public class No33 {

    /**
     * 2. 模拟
     */
    public int search2(int[] nums, int target) {
        int left = 0, right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) return mid;

            if (nums[mid] < nums[right]) {
                if (nums[mid] < target && target <= nums[right]) left = mid + 1;
                else right = mid;
            } else {
                if (nums[left] <= target && target < nums[mid]) right = mid;
                else left = mid + 1;
            }
        }
        return -1;
    }
}
